package com.cx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.entity.ArticleComment;
import com.cx.mapper.ArticleCommentMapper;
import com.cx.service.IArticleCommentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@com.alibaba.dubbo.config.annotation.Service //：发布接口服务
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment> implements IArticleCommentService {

    @Override
    public IPage<ArticleComment> getArticleCommentListPageConditions(Map<String, Object> params) {
        IPage<ArticleComment> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        LambdaQueryWrapper<ArticleComment> clubLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Map<String, String> paramsMap = null;
        if (params.get("searchParams") != null) {
            String startDate = null,
                    endDate = null;
//          将获取的json格式的字符串转为Java中的对象
            paramsMap = (Map) (JSON.parse((String) params.get("searchParams")));
//          切割日期字符串
            if (!"".equals(paramsMap.get("created"))) {
                startDate = (paramsMap.get("created").split("~"))[0];
                endDate = (paramsMap.get("created").split("~"))[1];
            }

            clubLambdaQueryWrapper.eq(!"".equals(paramsMap.get("id")), ArticleComment::getId, paramsMap.get("id"))
                    .eq(!"".equals(paramsMap.get("articleId")), ArticleComment::getArticleId, paramsMap.get("articleId"))
                    .eq(!"".equals(paramsMap.get("userId")), ArticleComment::getUserId, paramsMap.get("userId"))
                    .like(!"".equals(paramsMap.get("plContent")), ArticleComment::getPlContent, paramsMap.get("plContent"))
                    .eq(!"".equals(paramsMap.get("pid")), ArticleComment::getPid, paramsMap.get("pid"))
                    .like(!"".equals(paramsMap.get("status")), ArticleComment::getStatus, paramsMap.get("status"))
                    .between(!"".equals(paramsMap.get("created")), ArticleComment::getCreated, startDate, endDate)
            ;
        }
        return this.baseMapper.getClubListPageConditions(page, clubLambdaQueryWrapper);
    }

    public List<ArticleComment> findarticleIdCommentList(Integer articleId, Integer pid) {

        //根据文章id查询全部评论,pid为0，查询顶级评论
//        List<ArticleComment> parentComments = this.list(new LambdaQueryWrapper<ArticleComment>()
        List<ArticleComment> parentComments = this.baseMapper.selectArticleCommentsUser(new LambdaQueryWrapper<ArticleComment>()
                .eq(ArticleComment::getArticleId, articleId)
                .eq(ArticleComment::getPid, pid));// pid为0表示顶级评论
        List<ArticleComment> comments = new ArrayList<>();

        // 递归构建评论树
        parentComments.forEach(parentComment -> {
            buildChildren(articleId, parentComment);
            comments.add(parentComment);
        });
        return comments;
    }

    private void buildChildren(Integer articleId, ArticleComment parentComment) {
//        List<ArticleComment> children = this.list(new LambdaQueryWrapper<ArticleComment>()
        List<ArticleComment> children = this.baseMapper.selectArticleCommentsUser(new LambdaQueryWrapper<ArticleComment>()
                .eq(ArticleComment::getArticleId, articleId)
                .eq(ArticleComment::getPid, parentComment.getId()));

        children.forEach(child -> {
            parentComment.addChild(child);
            buildChildren(articleId, child); // 递归调用以构建更深层次的子评论
        });
    }

//    List<ArticleComment> parentComments = articleCommentMapper.selectList(queryWrapper);


}
